package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuQuery;
import com.spzx.product.api.domain.vo.SkuPriceVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品规格管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService productSkuService;

    @Operation(summary = "分页带条件的查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(Product product){
        List<Product> list=productService.getPageList(product);
        return getDataTable(list);
    }

    @Operation(summary ="添加商品")
    @PostMapping
    public AjaxResult saveProduct(@RequestBody Product product){
        boolean save= productService.saveProduct(product);
        return toAjax(save);
    }

    @Operation(summary = "根据id查询商品详情")
    @GetMapping("/{id}")
    public AjaxResult getProductById(@PathVariable Long id ){

        Product product=productService.getProductById(id);
        return success(product);
    }

    @Operation(summary = "根据id更新商品")
    @PutMapping
    public AjaxResult saveProductById(@RequestBody Product product){
        boolean update = productService.saveProductById(product);
        return toAjax(update);
    }

    @Operation(summary = "根据id删除和批量删除")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductById(@PathVariable List<Long> ids){
        productService.deleteProductById(ids);
        return success();
    }

    @Operation(summary = "根据id更新审核状态")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult getAuditStatusById(@PathVariable Long id,@PathVariable Integer auditStatus){
        productService.updateAuditStatusById(id,auditStatus);
        return success();
    }

    @Operation(summary = "根据id更新上架状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult getStatusById(@PathVariable Long id,@PathVariable Integer status){
        productService.getStatusById(id,status);
        return success();
    }

    @InnerAuth
    @Operation(summary = "远程调用接口,获取sku销量top20")
    @GetMapping("/getTopSale")
    public R<List<ProductSku>> getTopSale() {
        List<ProductSku> productSkuList=productService.getTopSale();
        return R.ok(productSkuList);
    }

    @InnerAuth
    @Operation(summary = "供微服务远程调用获取商品列表的接口")
    @GetMapping("/skuList/{pageNo}/{pageSize}")
    public R<TableDataInfo> getProducctSkuList(@PathVariable Integer pageNo , @PathVariable Integer pageSize, SkuQuery skuQuery){
        //开启分页
        PageHelper.startPage(pageNo,pageSize);
        List<ProductSku> productSkuList= productService.getProducctSkuList(skuQuery);
        return R.ok(getDataTable(productSkuList));
    }



    @Operation(summary = "供微服务远程调用获取商品sku信息")
    @InnerAuth
    @GetMapping(value = "/getProductSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId)
    {
        //调用自定义的根据id查询方法
        return R.ok(productSkuService.getProductSkuById(skuId));
    }

    @Operation(summary = "获取商品信息")
    @InnerAuth
    @GetMapping(value = "/getProduct/{id}")
    public R<Product> getProduct(@PathVariable("id") Long id)
    {
        //调用自定义的根据id查询方法
        return R.ok(productService.getProductInfoById(id));
    }

    @Operation(summary = "获取商品sku最新价格信息")
    @InnerAuth
    @GetMapping(value = "/getSkuPrice/{skuId}")
    public R<SkuPriceVo> getSkuPrice(@PathVariable("skuId") Long skuId)
    {
        SkuPriceVo skuPriceVo= productService.getSkuPrice(skuId);
        return R.ok(skuPriceVo);
    }

    @Operation(summary = "获取商品详细图片信息")
    @InnerAuth
    @GetMapping(value = "/getProductDetails/{id}")
    public R<ProductDetails> getProductDetails(@PathVariable("id") Long id)
    {
        return R.ok(productService.getProductDetails(id));
    }

    @Operation(summary = "获取商品sku规则详细信息")
    @InnerAuth
    @GetMapping(value = "/getSkuSpecValue/{id}")
    public R<Map<String, Long>> getSkuSpecValue(@PathVariable("id") Long id)
    {
        return R.ok(productService.getSkuSpecValue(id));
    }

    @Operation(summary = "获取商品sku库存信息")
    @InnerAuth
    @GetMapping(value = "/getSkuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId)
    {
        return R.ok(productService.getSkuStock(skuId));
    }



}
